﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Public;
using BUL;

namespace Presentation
{
    public partial class frmPaymentTransaction : Form
    {
        Role role = new Role();
        User u = new User();

        public frmPaymentTransaction()
        {
            InitializeComponent();
            this.ActiveControl = txtCardId;
            this.date.Value.GetDateTimeFormats();
            this.txtCardId.Text = "0";
            getUser();
            txtStaff.Text = u.UserId.ToString();
        }

        private void getUser()
        {
            role.RoleId = 3;
            u = new User(role);
            u.UserId = 5;
        }

        PayTransactionBUL cls = new PayTransactionBUL();

        //bien dung cho su kien click row tren luoi
        int row = 0;

        //Su kien bam nut Check Card va hien thong tin khach hang
        private void btnCheck_Click_1(object sender, EventArgs e)
        {
            string cardId = txtCardId.Text;

            if (cls.CheckCard(txtCardId.Text).Rows.Count == 1)
            {
                this.txtCusName.Text = cls.getCustomer(txtCardId.Text).Rows[0][0].ToString();
                this.txtScore.Text = cls.getScore(txtCardId.Text).Rows[0][0].ToString();
                //MessageBox.Show("This is the LoyaltyCard!");
            }
            else if (txtCardId.Text == "")
            {
                this.txtCusName.Clear();
                this.txtScore.Clear();
                MessageBox.Show("Input CardId please!");
            }
            else
            {
                this.txtCusName.Clear();
                this.txtScore.Clear();
                MessageBox.Show("This is NOT the LoyaltyCard!");
            }
        }

        //Validate gia tri nhap vao o cardId
        private void txtCardId_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Input numbers please!", "Information Message");
            }
            else
            {
                e.Handled = false;
            }
        }

        //Su kien bam nut Cancel, dong cua so chuong trinh, ko luu data
        private void btnCancel_Click(object sender, EventArgs e)
        {
            frmMain main = new frmMain();
            main.Show();
            this.Hide();
        }


        private void dgvStaff_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            row = e.RowIndex;
        }

        private void dgvStaff_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                string inputedBarcode = dgvStaff.Rows[row].Cells[0].Value.ToString();
                dgvStaff.Rows[row].Cells[1].Value = cls.getProductName(inputedBarcode).Rows[0][0];
                dgvStaff.Rows[row].Cells[3].Value = cls.getUnitPrice(inputedBarcode).Rows[0][0];
                dgvStaff.Rows[row].Cells[5].Value = cls.getProductId(inputedBarcode).Rows[0][0];
            }
            catch
            {
            }
            if (dgvStaff.Rows[row].Cells[2].Value != null && dgvStaff.Rows[row].Cells[3].Value != null)
            {
                dgvStaff.Rows[row].Cells[4].Value = double.Parse(dgvStaff.Rows[row].Cells[2].Value.ToString()) * double.Parse(dgvStaff.Rows[row].Cells[3].Value.ToString());
            }
            int sum = 0;
            for (int i = 0; i < dgvStaff.Rows.Count; ++i)
            {
                sum += Convert.ToInt32(dgvStaff.Rows[i].Cells[4].Value);
            }
            txtTotal.Text = sum.ToString();

        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dgvStaff.SelectedRows)
            {
                if (!row.IsNewRow)
                    dgvStaff.Rows.Remove(row);
            }
            int sum = 0;
            for (int i = 0; i < dgvStaff.Rows.Count; ++i)
            {
                sum += Convert.ToInt32(dgvStaff.Rows[i].Cells[4].Value);
            }
            txtTotal.Text = sum.ToString();
        }


        private void btnCheckOut_Click(object sender, EventArgs e)
        {
            Invoice i = new Invoice();
            i.CardId = int.Parse(txtCardId.Text);
            i.CashierId = int.Parse(txtStaff.Text);
            i.PaymentDate = date.Value;
            i.TotalPrice = float.Parse(txtTotal.Text);
            try
            {
                cls.AddInvoice(i);
               //MessageBox.Show("A new invoice has been created sucessfully!");
            }
            catch { }
            try
            {
                Purchasing p = new Purchasing();
                for (int j = 0; j < dgvStaff.Rows.Count - 1; j++)
                {
                    p.InvoiceId = int.Parse(cls.getMaxInvoiceId(txtStaff.Text).Rows[0][0].ToString());
                    p.NumOfProduct = int.Parse(dgvStaff.Rows[j].Cells[2].Value.ToString());
                    p.ProductId = int.Parse(dgvStaff.Rows[j].Cells[5].Value.ToString());
                    p.ProductTotal = int.Parse(dgvStaff.Rows[j].Cells[4].Value.ToString());
                    cls.AddPurchasing(p);
                    //MessageBox.Show("the new parchasing has been created sucessfully!");
                }
            }
            catch
            {
            }
            if (txtScore.Text != "0")
            {
                try
                {
                    LoyaltyCard l = new LoyaltyCard();
                    l.Score = (int)(double.Parse(txtScore.Text.ToString()) + (double.Parse(txtTotal.Text.ToString())) * 0.001);
                    l.CardId = int.Parse(txtCardId.Text);
                    cls.UpdateScore(l);
                    //MessageBox.Show("Update Score sucessfully!");
                }
                catch { }
            }
            else { }

            Product pq = new Product();
            for (int t = 0; t < dgvStaff.Rows.Count - 1; t++)
            {
                pq.BarCode = dgvStaff.Rows[t].Cells[0].Value.ToString();
                pq.Quantity = int.Parse(dgvStaff.Rows[t].Cells[2].Value.ToString());
                cls.UpdateQuantity(pq);
            }
            MessageBox.Show("Invoice has been created sucessfully!");
        }

        private void btnNewInvoice_Click(object sender, EventArgs e)
        {
            this.txtCardId.Text = "0";
            this.txtCusName.Clear();
            this.txtScore.Clear();
            this.txtTotal.Clear();
            dgvStaff.Rows.Clear();
        }

        private void frmPaymentTransaction_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult rs = MessageBox.Show("Are you really want to exist?", "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (rs == DialogResult.OK)
            {
                frmMain main = new frmMain();
                this.Hide();
                main.Show();
            }
            else
            {
                e.Cancel = true;
            }
        }
    }
}

